home *** CD-ROM | disk | FTP | other *** search
- Path: qualcomm.com!usenet
- From: nabbasi@qualcomm.com (Nasser Abbasi)
- Newsgroups: comp.lang.c++
- Subject: Re: Derived Class Function ?
- Date: 5 Feb 1996 06:40:39 GMT
- Organization: QUALCOMM
- Message-ID: <4f48p7$rpq@qualcomm.com>
- References: <toddosDM9o9r.Ezw@netcom.com>
- NNTP-Posting-Host: nabbasi.qualcomm.com
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.14
-
- In article <toddosDM9o9r.Ezw@netcom.com>, toddos@netcom.com says...
- >
- >Does anyone know a way to tell in a base class if a derived class
- >overrides a virtual function without calling the function? In other
- >words, I do not want to even call the base class implementation of a
- >function if a derived class does NOT override it. Thanks.
- >
- >Todd
- >
-
- Interesting question. May be you can try something like this quick hack
- below. Probably there is a more elegent way of doing
- it.
-
- Nasser
-
- ----------------------------- cut here -------------------------------
-
- #include <iostream.h>
-
- //
- // example how to tell in base class if a virtual function
- // in base class has been ovverriden. If so then call it, else
- // do not. (or di whatever you want)
- //
- // Use flag at construction time to indicate if a base class has
- // been extended by another class.
- //
- // Nasser Abbasi
- //
-
- #define true 1
- #define false 0
-
- typedef unsigned short int bool;
-
- class base
- {
- public:
- base(bool flag=false) : is_override (flag) {}
- virtual void foo() { cout<< "in base::foo() \n"; }
-
- void test() {
- // do a test to see if we should call a virtual
- // function or not. If it has been overriden, call it
- // else do not
- if(is_override)
- foo();
- else
- return;
- }
- private:
- bool is_override;
- };
-
- class derived: public base
- {
- public:
- derived(): base(true) {}
- void foo() { cout<< "in derived::foo() \n"; }
- };
-
- main()
- {
- base obj;
- derived obj_1;
-
- cout<<"befor calling obj.test()\n";
- obj.test(); // this will NOT call base::foo() since it is not
- overriden
-
- cout<<"befor callling obj_1.test()\n";
- obj_1.test(); // this WILL call derived::foo() since it is overriden
-
- return 1;
- }
-
-
-
-